var oo = {
	host: window.location.hostname,
	adaptive: function(params) {
		if(!params) {
			params = {'num': 6, 'id': 'specialList'};
		}
		if(typeof(params.num) == 'undefined') params.num = 6;
		if(typeof(params.id) == 'undefined') params.id = 'specialList';
		var self = this;
		$(window).load(function(){self.doAdaptive(params);});
		$(window).resize(function(){self.doAdaptive(params);});
	},
	doAdaptive: function(params) {	
		var num = params.num; // 每排图片数
		var w = $('#' + params.id).width() / num;
		$('#' + params.id + ' li').css({'width': w + 'px'});
		$('#' + params.id + ' img').css({'width': (w - 38) + 'px'});
		
		// 重新排版
		var h1 = Array();
		var pos = Array();
		$('#' + params.id + ' li').each(function(i){
			var row = Math.floor(i / num);
			var m = i % num;
			var l = m * w;
			var hx = $(this).height();
			if(typeof(h1[m]) == 'undefined') {
				h1[m] = hx;
			} else {
				h1[m] = h1[m] + hx;
			}
			if(row == 0) {
				pos[i] = {left:l + 'px', top: '0px'};
			} else {
				pos[i] = {left:l + 'px', top: (h1[m] - hx) + 'px'};
			}
		});
		eval('var maxHeight = Math.max(' + h1.join(',') + ');');
		$('#' + params.id).css({'height': maxHeight + 'px'});
		$('#' + params.id + ' li').each(function(i){
			var l = pos[i]['left'];
			var t = pos[i]['top'];
			$(this).css({position:'absolute',left:l,top:t});
		});
	},
	addFavorite: function(){
		var Title = document.title;
		var url = window.location.href;
		try{
			window.external.addFavorite(url, Title);
			return false;
		}catch(e){
			try{
				window.sidebar.addPanel(Title, url, "");
				return false;
			}catch (e){
				alert(e.message);
				return false;
			}
		}
	},	
	setHomepage: function(obj){
		var url = 'http://' + this.host + '/';
		try{
			obj.style.behavior='url(#default#homepage)';
			obj.setHomePage(url);
			return false;
		}catch(e){
			if(window.netscape) {
				try {
					netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
				}catch(e){
					//alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入“about:config”并回车\n然后将 [signed.applets.codebase_principal_support]的值设置为'true',双击即可。");
					alert(e.message);
					return false;
				}
				var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
				prefs.setCharPref('browser.startup.homepage', url);
				return false;
			}
			return false;
		}
	},
	showBg: function() {
		var self = this;
		var obj = $('img.bgcache');
		if(obj.length == 0) return;
		var bg = obj.attr('src');
		if(/[a-f0-9]{16,}/i.test(bg)) {
			$('body').css({'background': '#000000'});
			$('#mask').css({'background': 'url(' + bg + ')'});
			setTimeout(function(){self.setBg();}, 8000);
		} else {
			self.setBg();
		}
	},
	setBg: function() {
		var obj = $('img.bgcache');
		if(obj.length == 0) return;
		var params = obj.attr('rel').split('?');
		var url = params[0];
		var data = params[1];
		data += '&w=' + $(window).width();
		data += '&h=' + $(window).height();
		data += '&ns=true';
		$.ajax({type:'POST',dataType:'json',url:url,data:data + '&r=' + Math.random().toString(),success:function(json, status){
			try {
				obj.attr('src', json.url);
			} catch (e) {
				ajaxError(e.message);
			}
		}, error: function (req, msg, e) {
			ajaxError(msg);
		}, timeout: 30000});
		
		var self = this;
		function ajaxError(msg) {
			setTimeout(function(){self.setBg();}, 1000);
		}
	},
	uc: {
		init: function() {
			var self = this;
			$('#footer').hide();
			$(window).resize(function(){self.resize();}).resize();			
		},
		resize: function() {
			var h = $(window).height();
			$('.sidebar').css({'min-height': h + 'px'});
			$('.menu').css({'min-height': h + 'px', 'padding-top': '55px', 'position': 'fixed'});
		}
	}
};
$('.specialList').each(function(){
	var id = '__list_' + Math.random().toString().replace('.', '_');
	$(this).attr('id', id);
	oo.adaptive({'num': 5, 'id': id});
});